---
title: "Middleware"
description: "Core middleware system for MCP requests API Documentation"
icon: "code"
github: "https://github.com/mcp-use/mcp-use/blob/main/libraries/python/mcp_use/client/middleware/middleware.py"
---

import {RandomGradientBackground} from "/snippets/gradient.jsx"

<Callout type="info" title="Source Code">
View the source code for this module on GitHub: <a href='https://github.com/mcp-use/mcp-use/blob/main/libraries/python/mcp_use/client/middleware/middleware.py' target='_blank' rel='noopener noreferrer'>https://github.com/mcp-use/mcp-use/blob/main/libraries/python/mcp_use/client/middleware/middleware.py</a>
</Callout>

Core middleware system for MCP requests.

This module provides a robust and extensible middleware architecture:
- A typed MiddlewareContext to carry request data.
- A Middleware base class with a dispatcher that routes to strongly-typed hooks.
- A MiddlewareManager to build and execute the processing chain.
- A CallbackClientSession that acts as an adapter, creating the initial context
  without requiring changes to upstream callers like HttpConnector.

## CallbackClientSession

<div>
<RandomGradientBackground className="rounded-lg p-4 w-full h-full rounded-full">
<div className="text-black">
<div className="text-black font-bold text-xl mb-2 mt-8"><code className="!text-black">class</code> CallbackClientSession</div>

ClientSession wrapper that uses callback-based middleware.

</div>
</RandomGradientBackground>
```python
from mcp_use.client.middleware.middleware import CallbackClientSession
```

<Card type="info">
### `method` __init__

**Parameters**
><ParamField body="client_session" type="mcp.client.session.ClientSession" required="True" >   MCP session instance </ParamField>
><ParamField body="connector_id" type="str" required="True" >   Connector instance </ParamField>
><ParamField body="middleware_manager" type="mcp_use.client.middleware.middleware.MiddlewareManager" required="True" >   Middleware instance </ParamField>

**Signature**
```python wrap
def __init__(client_session: mcp.client.session.ClientSession, connector_id: str, middleware_manager: mcp_use.client.middleware.middleware.MiddlewareManager):
```

</Card>
<Card type="info">
### `method` call_tool


**Parameters**
><ParamField body="name" type="str" required="True" >   Name identifier </ParamField>
><ParamField body="arguments" type="dict[str, Any]" required="True" >   Dictionary of key-value pairs </ParamField>
><ParamField body="args" required="True" >   Parameter value </ParamField>
><ParamField body="kwargs" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.CallToolResult" />

**Signature**
```python wrap
def call_tool(name: str, arguments: dict[str, Any], args, kwargs):
```

</Card>

<Card type="info">
### `method` get_prompt


**Parameters**
><ParamField body="name" type="str" required="True" >   Name identifier </ParamField>
><ParamField body="args" required="True" >   Parameter value </ParamField>
><ParamField body="kwargs" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.GetPromptResult" />

**Signature**
```python wrap
def get_prompt(name: str, args, kwargs):
```

</Card>

<Card type="info">
### `method` initialize


**Parameters**
><ParamField body="args" required="True" >   Parameter value </ParamField>
><ParamField body="kwargs" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.InitializeResult" />

**Signature**
```python wrap
def initialize(args, kwargs):
```

</Card>

<Card type="info">
### `method` list_prompts


**Parameters**
><ParamField body="args" required="True" >   Parameter value </ParamField>
><ParamField body="kwargs" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.ListPromptsResult" />

**Signature**
```python wrap
def list_prompts(args, kwargs):
```

</Card>

<Card type="info">
### `method` list_resources


**Parameters**
><ParamField body="args" required="True" >   Parameter value </ParamField>
><ParamField body="kwargs" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.ListResourcesResult" />

**Signature**
```python wrap
def list_resources(args, kwargs):
```

</Card>

<Card type="info">
### `method` list_tools


**Parameters**
><ParamField body="args" required="True" >   Parameter value </ParamField>
><ParamField body="kwargs" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.ListToolsResult" />

**Signature**
```python wrap
def list_tools(args, kwargs):
```

</Card>

<Card type="info">
### `method` read_resource


**Parameters**
><ParamField body="uri" type="str" required="True" >   String value </ParamField>
><ParamField body="args" required="True" >   Parameter value </ParamField>
><ParamField body="kwargs" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.ReadResourceResult" />

**Signature**
```python wrap
def read_resource(uri: str, args, kwargs):
```

</Card>

</div>

## MCPResponseContext

<div>
<RandomGradientBackground className="rounded-lg p-4 w-full h-full rounded-full">
<div className="text-black">
<div className="text-black font-bold text-xl mb-2 mt-8"><code className="!text-black">class</code> MCPResponseContext</div>

Extended context for MCP responses with middleware metadata.

</div>
</RandomGradientBackground>
```python
from mcp_use.client.middleware.middleware import MCPResponseContext
```

<Card type="info">
**Attributes**
>
<ParamField body="request_id" type="str" required="True" >   String value </ParamField>
<ParamField body="result" type="Any" required="True" >   Parameter value </ParamField>
<ParamField body="error" type="Exception | None" required="True" >   Parameter value </ParamField>
<ParamField body="duration" type="float" required="True" >   Parameter value </ParamField>
<ParamField body="metadata" type="dict[str, Any]" required="True" >   Dictionary of key-value pairs </ParamField>
<ParamField body="jsonrpc_response" type="mcp.types.JSONRPCResponse | None" required="True" >   Parameter value </ParamField>

</Card>

<Card type="info">
### `method` __init__

**Parameters**
><ParamField body="request_id" type="str" required="True" >   String value </ParamField>
><ParamField body="result" type="Any" required="True" >   Parameter value </ParamField>
><ParamField body="error" type="Exception | None" required="True" >   Parameter value </ParamField>
><ParamField body="duration" type="float" required="True" >   Parameter value </ParamField>
><ParamField body="metadata" type="dict[str, Any]" required="True" >   Dictionary of key-value pairs </ParamField>
><ParamField body="jsonrpc_response" type="mcp.types.JSONRPCResponse | None" default="None" >   Parameter value </ParamField>

**Signature**
```python wrap
def __init__(request_id: str, result: Any, error: Exception | None, duration: float, metadata: dict[str, Any], jsonrpc_response: mcp.types.JSONRPCResponse | None = None):
```

</Card>
</div>

## Middleware

<div>
<RandomGradientBackground className="rounded-lg p-4 w-full h-full rounded-full">
<div className="text-black">
<div className="text-black font-bold text-xl mb-2 mt-8"><code className="!text-black">class</code> Middleware</div>

Base class for middlewares with hooks.

</div>
</RandomGradientBackground>
```python
from mcp_use.client.middleware.middleware import Middleware
```

<Card type="info">
### `method` __call__

Main entry point that orchestrates the chain


**Parameters**
><ParamField body="context" type="mcp_use.client.middleware.middleware.MiddlewareContext" required="True" >   Parameter value </ParamField>
><ParamField body="call_next" type="mcp_use.client.middleware.middleware.NextFunctionT" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="Any" />

**Signature**
```python wrap
def __call__(
context: mcp_use.client.middleware.middleware.MiddlewareContext,
    call_next: mcp_use.client.middleware.middleware.NextFunctionT
):
```

</Card>

<Card type="info">
### `method` on_call_tool


**Parameters**
><ParamField body="context" type="mcp_use.client.middleware.middleware.MiddlewareContext" required="True" >   Parameter value </ParamField>
><ParamField body="call_next" type="mcp_use.client.middleware.middleware.NextFunctionT" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.CallToolResult" />

**Signature**
```python wrap
def on_call_tool(
context: mcp_use.client.middleware.middleware.MiddlewareContext,
    call_next: mcp_use.client.middleware.middleware.NextFunctionT
):
```

</Card>

<Card type="info">
### `method` on_get_prompt


**Parameters**
><ParamField body="context" type="mcp_use.client.middleware.middleware.MiddlewareContext" required="True" >   Parameter value </ParamField>
><ParamField body="call_next" type="mcp_use.client.middleware.middleware.NextFunctionT" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.GetPromptResult" />

**Signature**
```python wrap
def on_get_prompt(
context: mcp_use.client.middleware.middleware.MiddlewareContext,
    call_next: mcp_use.client.middleware.middleware.NextFunctionT
):
```

</Card>

<Card type="info">
### `method` on_initialize


**Parameters**
><ParamField body="context" type="mcp_use.client.middleware.middleware.MiddlewareContext" required="True" >   Parameter value </ParamField>
><ParamField body="call_next" type="mcp_use.client.middleware.middleware.NextFunctionT" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.InitializeResult" />

**Signature**
```python wrap
def on_initialize(
context: mcp_use.client.middleware.middleware.MiddlewareContext,
    call_next: mcp_use.client.middleware.middleware.NextFunctionT
):
```

</Card>

<Card type="info">
### `method` on_list_prompts


**Parameters**
><ParamField body="context" type="mcp_use.client.middleware.middleware.MiddlewareContext" required="True" >   Parameter value </ParamField>
><ParamField body="call_next" type="mcp_use.client.middleware.middleware.NextFunctionT" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.ListPromptsResult" />

**Signature**
```python wrap
def on_list_prompts(
context: mcp_use.client.middleware.middleware.MiddlewareContext,
    call_next: mcp_use.client.middleware.middleware.NextFunctionT
):
```

</Card>

<Card type="info">
### `method` on_list_resources


**Parameters**
><ParamField body="context" type="mcp_use.client.middleware.middleware.MiddlewareContext" required="True" >   Parameter value </ParamField>
><ParamField body="call_next" type="mcp_use.client.middleware.middleware.NextFunctionT" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.ListResourcesResult" />

**Signature**
```python wrap
def on_list_resources(
context: mcp_use.client.middleware.middleware.MiddlewareContext,
    call_next: mcp_use.client.middleware.middleware.NextFunctionT
):
```

</Card>

<Card type="info">
### `method` on_list_tools


**Parameters**
><ParamField body="context" type="mcp_use.client.middleware.middleware.MiddlewareContext" required="True" >   Parameter value </ParamField>
><ParamField body="call_next" type="mcp_use.client.middleware.middleware.NextFunctionT" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.ListToolsResult" />

**Signature**
```python wrap
def on_list_tools(
context: mcp_use.client.middleware.middleware.MiddlewareContext,
    call_next: mcp_use.client.middleware.middleware.NextFunctionT
):
```

</Card>

<Card type="info">
### `method` on_read_resource


**Parameters**
><ParamField body="context" type="mcp_use.client.middleware.middleware.MiddlewareContext" required="True" >   Parameter value </ParamField>
><ParamField body="call_next" type="mcp_use.client.middleware.middleware.NextFunctionT" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp.types.ReadResourceResult" />

**Signature**
```python wrap
def on_read_resource(
context: mcp_use.client.middleware.middleware.MiddlewareContext,
    call_next: mcp_use.client.middleware.middleware.NextFunctionT
):
```

</Card>

<Card type="info">
### `method` on_request


**Parameters**
><ParamField body="context" type="mcp_use.client.middleware.middleware.MiddlewareContext" required="True" >   Parameter value </ParamField>
><ParamField body="call_next" type="mcp_use.client.middleware.middleware.NextFunctionT" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="Any" />

**Signature**
```python wrap
def on_request(
context: mcp_use.client.middleware.middleware.MiddlewareContext,
    call_next: mcp_use.client.middleware.middleware.NextFunctionT
):
```

</Card>

</div>

## MiddlewareContext

<div>
<RandomGradientBackground className="rounded-lg p-4 w-full h-full rounded-full">
<div className="text-black">
<div className="text-black font-bold text-xl mb-2 mt-8"><code className="!text-black">class</code> MiddlewareContext</div>

Unified, typed context for all middleware operations.

</div>
</RandomGradientBackground>
```python
from mcp_use.client.middleware.middleware import MiddlewareContext
```

<Card type="info">
**Attributes**
>
<ParamField body="id" type="str" required="True" >   String value </ParamField>
<ParamField body="method" type="str" required="True" >   String value </ParamField>
<ParamField body="params" type="mcp_use.client.middleware.middleware.T" required="True" >   Parameter value </ParamField>
<ParamField body="connection_id" type="str" required="True" >   String value </ParamField>
<ParamField body="timestamp" type="float" required="True" >   Parameter value </ParamField>
<ParamField body="metadata" type="dict[str, Any]" required="True" >   Dictionary of key-value pairs </ParamField>

</Card>

<Card type="info">
### `method` __init__

**Parameters**
><ParamField body="id" type="str" required="True" >   String value </ParamField>
><ParamField body="method" type="str" required="True" >   String value </ParamField>
><ParamField body="params" type="mcp_use.client.middleware.middleware.T" required="True" >   Parameter value </ParamField>
><ParamField body="connection_id" type="str" required="True" >   String value </ParamField>
><ParamField body="timestamp" type="float" required="True" >   Parameter value </ParamField>
><ParamField body="metadata" type="dict[str, Any]" default="<factory>" >   Dictionary of key-value pairs </ParamField>

**Signature**
```python wrap
def __init__(id: str, method: str, params: mcp_use.client.middleware.middleware.T, connection_id: str, timestamp: float, metadata: dict[str, Any] = <factory>):
```

</Card>
</div>

## MiddlewareManager

<div>
<RandomGradientBackground className="rounded-lg p-4 w-full h-full rounded-full">
<div className="text-black">
<div className="text-black font-bold text-xl mb-2 mt-8"><code className="!text-black">class</code> MiddlewareManager</div>

Manages middleware callbacks for MCP requests.

</div>
</RandomGradientBackground>
```python
from mcp_use.client.middleware.middleware import MiddlewareManager
```

<Card type="info">
### `method` __init__

**Signature**
```python wrap
def __init__():
```

</Card>
<Card type="info">
### `method` add_middleware

Add a middleware callback.


**Parameters**
><ParamField body="callback" type="mcp_use.client.middleware.middleware.Middleware" required="True" >   Callback function </ParamField>

**Signature**
```python wrap
def add_middleware(callback: mcp_use.client.middleware.middleware.Middleware):
```

</Card>

<Card type="info">
### `method` process_request

Runs the full middleware chain, captures timing and errors,
        and returns a structured MCPResponseContext.


**Parameters**
><ParamField body="context" type="mcp_use.client.middleware.middleware.MiddlewareContext" required="True" >   Parameter value </ParamField>
><ParamField body="original_call" type="Callable" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp_use.client.middleware.middleware.MCPResponseContext" />

**Signature**
```python wrap
def process_request(
context: mcp_use.client.middleware.middleware.MiddlewareContext,
    original_call: Callable
):
```

</Card>

</div>

## NextFunctionT

<div>
<RandomGradientBackground className="rounded-lg p-4 w-full h-full rounded-full">
<div className="text-black">
<div className="text-black font-bold text-xl mb-2 mt-8"><code className="!text-black">class</code> NextFunctionT</div>

Protocol for the `call_next` function passed to middleware.

</div>
</RandomGradientBackground>
```python
from mcp_use.client.middleware.middleware import NextFunctionT
```

<Card type="info">
### `method` __init__

**Parameters**
><ParamField body="args" required="True" >   Parameter value </ParamField>
><ParamField body="kwargs" required="True" >   Parameter value </ParamField>

**Signature**
```python wrap
def __init__(args, kwargs):
```

</Card>
<Card type="info">
### `method` __call__


**Parameters**
><ParamField body="context" type="mcp_use.client.middleware.middleware.MiddlewareContext" required="True" >   Parameter value </ParamField>

**Returns**
><ResponseField name="returns" type="mcp_use.client.middleware.middleware.R" />

**Signature**
```python wrap
def __call__(context: mcp_use.client.middleware.middleware.MiddlewareContext):
```

</Card>

</div>
